package com.zhuiyi.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyi.zhxy.pojo.Student;
import com.zhuiyi.zhxy.service.StudentService;
import com.zhuiyi.zhxy.util.MD5;
import com.zhuiyi.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2023/1/22 21:05
 **/
@Api(tags = "学生管理器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //Request URL: http://localhost:9001/sms/studentController/delStudentById
    //Request Method: DELETE
    @ApiOperation("删除单个或多个学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(@ApiParam("JSON的要删除id的数组")@RequestBody List<Integer> ids){
        studentService.removeByIds(ids);
        return Result.ok();
    }

    //sms/studentController/addOrUpdateStudent
    @ApiOperation("新增或修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@ApiParam("JSON的student对象")@RequestBody Student student){
        //新增的密码的密码是明文要加密，修改时密码本来就是密文不用加密
        //新增没有id，修改有id
        if (null == student.getId() || 0 == student.getId()) {
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);

        return Result.ok();
    }

    //getStudentByOpr/1/3?name=cc&clazzName=
    @ApiOperation("分页带条件查询student")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @ApiParam("页码数")@PathVariable("pageNo") Integer pageNo,
            @ApiParam("页大小")@PathVariable("pageSize") Integer pageSize,
            @ApiParam("分页查询条件的学生信息") Student student){

        Page<Student> page = new Page<>(pageNo,pageSize);
        IPage<Student> iPage = studentService.getStudentByOpr(page,student);
        return Result.ok(iPage);
    }
}
